<template>
    <el-card class="box-card" shadow="always">
        <el-form label-width="70px" class="login-container" :inline="true" ref="form" :rules="rules" :model="form">
            <h3 class="login_title">登录系统</h3>
            <el-form-item label="用户名" prop="username">
                <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">登录</el-button>

            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>

import { getMenu } from '../api'

import Cookie from 'js-cookie'
export default {
    data() {
        return {
            form: {
                username: 'admin',
                password: 'admin'
            }, rules: {
                username: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
            }
        }
    },
    methods: {

        onSubmit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    getMenu(this.form).then(({ data }) => {
                        if (data.code === 20000) {
                            this.$store.state.tab.username = data.username
                            localStorage.setItem('userName', JSON.stringify(this.$store.state.tab.username))
                            this.$store.state.tab.userId = data.userId
                            this.$store.state.tab.userCard = data.userCard
                            Cookie.set('token', data, data.token)
                            this.$store.commit('setMenu', data.data.menu)
                            this.$store.commit('addMenu', this.$router)
                            this.$router.push('/home')


                        } else {
                            this.$message.error('用户名密码输入错误，请重新输入！')
                        }
                    })
                }

            })

        },

    }
}
</script>

<style lang="less" scoped>
.box-card {
    width: 350px;
    margin: 180px auto;
    padding: 35px 35px 15px 35px;
    border-radius: 15px;

    .login_title {
        text-align: center;
        margin-bottom: 40px;
        color: #505458
    }

    .el-input {
        width: 198px;
    }

    .el-button {
        margin-left: 105px;
    }

}
</style>